package com.ccjk.beusoft.plugin.lib;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.WindowManager;

/**
 * Created by CCJK on 2017/12/2.
 */

public class HostProxyActivity extends AppCompatActivity implements Bind {
    private ActivityProxy activityProxy = new ActivityProxy(this);
    private PluginActivityInterface pluginActivity;

    //
    public void bind(Activity activity, PluginPackage pluginPackage) {
        pluginActivity = (PluginActivityInterface) activity;

    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityProxy.onCreate(getIntent());
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    public void onDestroy() {
        super.onDestroy();
        if (pluginActivity != null)
            pluginActivity.onDestroy();
    }


    public void onResume() {
        super.onResume();
        if (pluginActivity != null)
            pluginActivity.onResume();
    }

    public void onPause() {
        super.onPause();
        if (pluginActivity != null)
            pluginActivity.onPause();
    }

    public void onRestart() {
        super.onRestart();
        if (pluginActivity != null)
            pluginActivity.onRestart();
    }

    public void onStart() {
        super.onStart();
        if (pluginActivity != null)
            pluginActivity.onStart();
    }

    public void onStop() {
        super.onStop();
        if (pluginActivity != null)
            pluginActivity.onStop();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (pluginActivity != null)
            pluginActivity.onActivityResult(requestCode, resultCode, data);
    }

    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (pluginActivity != null)
            pluginActivity.onSaveInstanceState(outState);
    }

    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (pluginActivity != null)
            pluginActivity.onRestoreInstanceState(savedInstanceState);
    }

    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (pluginActivity != null)
            pluginActivity.onNewIntent(intent);
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (pluginActivity != null) {
            return pluginActivity.onTouchEvent(event) || super.onTouchEvent(event);
        }
        return super.onTouchEvent(event);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event)

    {
        if (pluginActivity != null) {
            return pluginActivity.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event);
        }
        return super.onKeyUp(keyCode, event);
    }

    public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
        super.onWindowAttributesChanged(params);
        if (pluginActivity != null)
            pluginActivity.onWindowAttributesChanged(params);
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (pluginActivity != null)
            pluginActivity.onWindowFocusChanged(hasFocus);
    }

    public void onBackPressed() {
        super.onBackPressed();
        if (pluginActivity != null)
            pluginActivity.onBackPressed();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        if (pluginActivity != null) {
            return pluginActivity.onCreateOptionsMenu(menu) || super.onCreateOptionsMenu(menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem item)


    {
        if (pluginActivity != null) {
            return pluginActivity.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
        }
        return super.onOptionsItemSelected(item);
    }

    public AssetManager getAssets()

    {

        return activityProxy.getPluginAssetManager() == null ? super.getAssets() : activityProxy.getPluginAssetManager();

    }

    public Resources getResources()

    {

        return activityProxy.getPluginResource() == null ? super.getResources() : activityProxy.getPluginResource();
    }

    @Override
    public Resources.Theme getTheme() {
        return activityProxy.getTheme() == null ? super.getTheme() : activityProxy.getTheme();
    }

}
